package com.yj.jason.jsondemo;

import android.animation.Animator;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.CommonRecyclerAdapter;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class TableActivity extends AppCompatActivity{

    private RecyclerView recyclerView;

    private TextView tv_more;

    private List<Person> mBeans=new ArrayList<>();

    private LinearLayoutManager mLayoutManager;

    private boolean isOnly=true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        recyclerView=findViewById(R.id.recyclerView);
        tv_more=findViewById(R.id.tv_more);

        for(int i=0;i<20;i++){
            Person person=new Person("总页防水等级"+i,i);
            mBeans.add(person);
        }

        mLayoutManager=new LinearLayoutManager(this);

        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.setAdapter(new CommonRecyclerAdapter<Person>(this,mBeans,R.layout.item_table) {
            @Override
            public void convert(ViewHolder holder, Person item) {
                holder.setText(R.id.tv_table,item.getName());
            }

        });



        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                 isOnly=true;
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                int totalItemCount = mLayoutManager.getItemCount();//总条目
                //lastVisibleItem >= totalItemCount - 5 表示剩下5个item实现预加载
                // dy>0 表示向下滑动,滑动距离

                Log.i("tag","lastVisibleItem==="+lastVisibleItem+"totalItemCount==="+totalItemCount);



                if (lastVisibleItem==(totalItemCount-1) && dy > 0&& isOnly) {

                    Toast.makeText(TableActivity.this,"加载更多",Toast.LENGTH_SHORT).show();
                    tv_more.setVisibility(View.VISIBLE);

                    tv_more.setTranslationY(100);

                    ViewPropertyAnimator animator=tv_more.animate();

                    animator.translationY(0);
                    animator.setDuration(800);
                    animator.start();

                    isOnly=false;
                }else if(dy<0){
                    ViewPropertyAnimator animator=tv_more.animate();

                    animator.translationY(100);
                    animator.setDuration(500);
                    animator.start();

                    animator.setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animator) {
                               //        tv_more.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animator) {

                        }
                    });
                }


            }

        });

    }


    //获取屏幕宽度
    private int getScreenWidht(Context mContext){
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}
